package com.wnxy.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.dto.AuthorizationDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

import com.wnxy.admin.entity.RolePermission;
import com.wnxy.admin.service.IRolePermissionService;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.spi.ResolveResult;

/**
 * <p>
 * 角色权限中间表 前端控制器
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/role-permission")
public class RolePermissionController {

    private final IRolePermissionService rolePermissionServiceImpl;
    public RolePermissionController(IRolePermissionService rolePermissionServiceImpl){
        this.rolePermissionServiceImpl = rolePermissionServiceImpl;
    }

    @PostMapping("/authorization")
    public ResponseResult authorization(@RequestBody AuthorizationDto authorizationDto){
        rolePermissionServiceImpl.authorization(authorizationDto);
        return ResponseResult.ok();
    }

    @GetMapping("/queryPermissionByRoleId")
    public ResponseResult queryPermissionByRoleId(Integer id){
        List<RolePermission> list = rolePermissionServiceImpl
                .list(Wrappers.lambdaQuery(RolePermission.class)
                        .eq(RolePermission::getRoleId,id));
        //只筛选出所有的权限id
        List<Integer> ids = list.stream().map(e -> e.getPermissionId()).collect(Collectors.toList());
        return ResponseResult.ok(ids);
    }

}
